package com.fjh.shop.goods.service.controller;

import com.fjh.shop.common.PageResult;
import com.fjh.shop.common.Result;
import com.fjh.shop.goods.model.vo.CategoryListVO;
import com.fjh.shop.goods.model.entity.Category;
import com.fjh.shop.goods.service.service.CategoryService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  商品分类前端控制器
 * </p>
 *
 * @author wuyunbin
 * @since 2025-07-16
 */
@Tag(name = "商品分类管理", description = "商品分类相关接口")
@Slf4j
@RestController
@RequestMapping("/category")
public class CategoryController {

    @Resource
    private CategoryService categoryService;

    /**
     * 添加商品分类
     * @param category 商品分类信息
     * @return 添加结果
     */
    @Operation(summary = "添加商品分类", description = "添加新的商品分类信息")
    @ApiResponse(responseCode = "200", description = "添加成功")
    @PostMapping("")
    public Result<Object> add(@RequestBody Category category) {
        log.info("添加商品分类：{}", category);
        categoryService.save(category);
        return Result.success();
    }

    /**
     * 根据ID查询商品分类
     * @param id 商品分类ID
     * @return 商品分类信息
     */
    @Operation(summary = "根据ID查询商品分类", description = "根据商品分类ID查询详细信息")
    @ApiResponse(responseCode = "200", description = "查询成功")
    @GetMapping("/{id}")
    public Result<Category> findById(@Parameter(description = "商品分类ID") @PathVariable Long id) {
        log.info("根据ID查询商品分类：{}", id);
        Category category = categoryService.getById(id);
        return Result.success(category);
    }

    /**
     * 分页查询商品分类列表
     * @param current 页码
     * @param pageSize 每页数量
     * @return 分页结果
     */
    @Operation(summary = "分页查询商品分类列表", description = "根据条件分页查询商品分类列表")
    @ApiResponse(responseCode = "200", description = "查询成功")
    @GetMapping("/page")
    public PageResult<Category> page(
            @Parameter(description = "页码") @RequestParam(defaultValue = "1") Integer current,
            @Parameter(description = "每页数量") @RequestParam(defaultValue = "10") Integer pageSize,
            @Parameter(description = "分类名称(模糊查询)") @RequestParam(required = false) String name,
            @Parameter(description = "状态") @RequestParam(required = false) Integer status) {
        log.info("分页查询商品分类列表：current={}, pageSize={}, name={}, status={}", current, pageSize, name, status);
        return PageResult.success(categoryService.pageQuery(current, pageSize, name, status));
    }

    /**
     * 更新商品分类
     * @param category 商品分类信息
     * @return 更新结果
     */
    @Operation(summary = "更新商品分类", description = "更新已有的商品分类信息")
    @ApiResponse(responseCode = "200", description = "更新成功")
    @PutMapping("")
    public Result<Boolean> update(@RequestBody Category category) {
        log.info("更新商品分类：{}", category);
        boolean result = categoryService.updateById(category);
        return Result.success(result);
    }

    /**
     * 删除商品分类
     * @param id 商品分类ID
     * @return 删除结果
     */
    @Operation(summary = "删除商品分类", description = "根据ID删除商品分类")
    @ApiResponse(responseCode = "200", description = "删除成功")
    @DeleteMapping("/{id}")
    public Result<Boolean> delete(@Parameter(description = "商品分类ID") @PathVariable Long id) {
        log.info("删除商品分类：{}", id);
        boolean result = categoryService.removeById(id);
        return Result.success(result);
    }


    @Operation(summary = "查询所有商品分类", description = "获取所有商品分类列表")
    @ApiResponse(responseCode = "200", description = "查询成功")
    @GetMapping("list")
    public Result<List<CategoryListVO>> list() {
        log.info("查询所有商品分类");
        List<CategoryListVO> list = categoryService.getList();
        return Result.success(list);
    }
}
